package part18;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;

/**
 * @author wcj
 * @Date 2021/6/4 16:17
 * @Version 1.0
 */
public class Part1826 {

    /**
     * ByteBuffer底层缓冲区的四个值：mark标记，position位置初始为0，limit,capacity初始时limit=capacity=缓冲区的容量
     * reset方法可以将position重置为mark
     *
     * @param args
     */
    public static void main(String[] args) {
        char[] data = "UsingBuffers".toCharArray();
        ByteBuffer buffer = ByteBuffer.allocate(data.length * 2);
        CharBuffer charBuffer = buffer.asCharBuffer();
        charBuffer.put(data);
        System.out.println(charBuffer.rewind());
        UsingBuffers.symmetricScramble(charBuffer);
        System.out.println(charBuffer.rewind());
        UsingBuffers.symmetricScramble(charBuffer);
        System.out.println(charBuffer.rewind());
    }
}

class UsingBuffers {
    public static void symmetricScramble(CharBuffer buffer) {
        while (buffer.hasRemaining()) {
            buffer.mark();
            char c1 = buffer.get();
            char c2 = buffer.get();
            buffer.reset();
            buffer.put(c2).put(c1);
        }
    }
}